﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    public float speed;
    private Rigidbody2D _rigidbody;
    private Vector3 changePos;
    private Animator _animator;

    private void Start()
    {
        _animator = GetComponent<Animator>();
        _rigidbody = GetComponent<Rigidbody2D>();
    }

    private void Update()
    {
        //changePos = Vector3.zero;
        UpdateAnimationAndMove();

        changePos.x = Input.GetAxisRaw("Horizontal");
        changePos.y = Input.GetAxisRaw("Vertical");
    }

    void UpdateAnimationAndMove()
    {
        if (changePos != Vector3.zero)
        {
            MoveCharacter();
            _animator.SetFloat("moveX", changePos.x);
            _animator.SetFloat("moveY", changePos.y);
            _animator.SetBool("moving", true);
        }
        else
        {
            _animator.SetBool("moving", false);
        }
    }

    void MoveCharacter()
    {
        _rigidbody.MovePosition(transform.position + changePos * speed * Time.deltaTime);
    }
}
